
package com.wyzz.hr.action;

import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.Status;
import com.wyzz.hr.bo.IEmpSalaryAcctversionBo;
import com.wyzz.hr.bo.ISalaryPaidBo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalaryacctversion;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.pojo.Empsalarypay;
import com.wyzz.hr.util.StringUtil;


public class DWRforSalaryPaid extends CompAction implements Status {

	public DWRforSalaryPaid() {
	}

	public String deleteSalaryPaid(String payId, HttpSession session) {
		String flt = DWRUtil.checkAuth("searchSalaryPaid", "execute");
		if ("error".equalsIgnoreCase(flt))
			return StringUtil.message(msgNoAuth, new Object[] { "noauth" });
		ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) getBean("salaryPaidBo");
		Empsalarypay empSalaryPaid = salaryPaidBo.loadSalaryPay(payId,
				new String[0]);
		if (empSalaryPaid == null)
			return StringUtil.message(msgNoPay, new Object[] { "nopay" });
		if (!salaryPaidBo.deleteSalaryPaid(empSalaryPaid))
			return StringUtil.message(msgOperErr, new Object[] { "fail",
					"\u85AA\u8D44\u53D1\u653E\u5220\u9664" });
		else
			return StringUtil.message(msgOperSucc, new Object[] { "SUCC",
					"\u85AA\u8D44\u53D1\u653E\u5220\u9664" });
	}

	public String updatePayOne(String salaryValue, String payId, String empId,
			String comment, String yearmonth) throws Exception {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("searchSalaryPaid",
				"execute")))
			return StringUtil.message(msgNoAuth, new Object[] { "noauth" });
		if (StringUtils.isEmpty(salaryValue))
			return StringUtil.message(msgNoPay, new Object[] { "nopay" });
		if (StringUtils.isEmpty(empId))
			return StringUtil.message(msgNoEmp, new Object[] { "noemp" });
		if (StringUtils.isEmpty(yearmonth))
			return StringUtil.message(msgNoYm, new Object[] { "noym" });
		if (!checkSalaryPeriod(yearmonth, new Integer[] { Integer.valueOf(0) }))
			return StringUtil.message(msgPeriodInitErr, new Object[0]);
		String arr$[] = salaryValue.split(",");
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			String salary = arr$[i$];
			if (!StringUtil.isFloat(salary,
					new Integer[] { Integer.valueOf(0) }))
				return StringUtil.message(msgFormatPay,
						new Object[] { "format" });
		}

		Empsalarypay pay = loadPay(payId, empId, yearmonth);
		setPayAdd(yearmonth, new Empsalarypay[] { pay });
		setPayValues(salaryValue, pay);
		interpretPay(yearmonth, false, new Empsalarypay[] { pay });
		if (pay == null)
			return StringUtil.message(msgNoPay, new Object[] { "null" });
		if (!StringUtils.isEmpty(comment)) {
			pay.setEspComment(comment.trim());
			pay.setEspChanged(Integer.valueOf(1));
		}
		Employee emp = pay.getEspEmpno();
		Empsalaryconfig config = emp.getConfig();
		pay.setEspEmpno(new Employee(emp.getId()));
		pay.setEspEmpconfig(new Empsalaryconfig(config.getId()));
		ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) getBean("salaryPaidBo");
		Empsalarypay payForPage = salaryPaidBo.calSalaryPayforPage(pay, pay
				.getAcctItems());
		StringBuffer strColumn = new StringBuffer("SUCC:");
		strColumn.append(payForPage.getShowColumn1()).append(",");
		strColumn.append(payForPage.getShowColumn4()).append(",");
		strColumn.append(payForPage.getShowColumn7()).append(",");
		strColumn.append(payForPage.getShowColumn8()).append(",");
		strColumn.append(payForPage.getShowColumn15()).append(",");
		strColumn.append(payForPage.getShowColumn18()).append(",");
		strColumn.append(payForPage.getShowColumn19()).append(",");
		salaryPaidBo.savePayAndInitEbp(pay);
		strColumn.append(pay.getEspChanged()).append(",");
		strColumn.append(pay.getId()).append(",");
		strColumn.append(pay.getEspBenefitPlans());
		return strColumn.toString();
	}

	public List initCalcPay(String payId, String empId, String yearmonth) {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("mySalaryPaid",
				"execute")))
			return null;
		if (StringUtils.isEmpty(payId) && StringUtils.isEmpty(empId))
			return null;
		if (StringUtils.isEmpty(yearmonth))
			return null;
		Empsalarypay pay = loadPay(payId, empId, yearmonth);
		if (pay.getId() == null
				&& checkSalaryPeriod(yearmonth, new Integer[] {
						Integer.valueOf(1), Integer.valueOf(2) }))
			return null;
		setPayAdd(yearmonth, new Empsalarypay[] { pay });
		if (pay.getId() == null)
			interpretPay(yearmonth, true, new Empsalarypay[] { pay });
		return getPayItemsPage(pay);
	}

	public List reCalcPayOne(String salaryValue, String espId, String empId,
			String yearmonth) {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("searchSalaryPaid",
				"execute")))
			return null;
		if (StringUtils.isEmpty(salaryValue))
			return null;
		if (StringUtils.isEmpty(empId))
			return null;
		if (StringUtils.isEmpty(yearmonth))
			return null;
		if (!checkSalaryPeriod(yearmonth, new Integer[] { Integer.valueOf(0) })) {
			return null;
		} else {
			Empsalarypay pay = loadPay(espId, empId, yearmonth);
			setPayAdd(yearmonth, new Empsalarypay[] { pay });
			setPayValues(salaryValue, pay);
			interpretPay(yearmonth, false, new Empsalarypay[] { pay });
			return getPayItemsPage(pay);
		}
	}

	public String[][] getBeneItemsByPay(String payId) {
		ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) getBean("salaryPaidBo");
		String result[][] = salaryPaidBo.getBeneItemsByPay(payId);
		return result;
	}

	public String getEsacNameByVersionId(String espEsavId) {
		Empsalaryacctversion version;
		try {
			IEmpSalaryAcctversionBo acctVersionBo = (IEmpSalaryAcctversionBo) getBean("empsalaryacctversionBo");
			version = acctVersionBo.loadObject(espEsavId,
					new String[] { "esavEsac" });
			if (version == null)
				return null;
		} catch (Exception e) {
			return "";
		}
		return version.getEsavEsac().getEsacName();
	}

	private static final long serialVersionUID = 0xc53a77b0ae03eebfL;
}
